<?php

namespace app\api\library;

use Exception;
use think\db\exception\ModelNotFoundException;
use think\Env;
use think\exception\Handle;

/**
 * 自定义API模块的错误显示
 */
class ExceptionHandle extends Handle
{

    public function render(Exception $e)
    {
        $code = 400;
        $msg = $e->getMessage();

        if ($e instanceof ModelNotFoundException) {
            $msg = '记录不存在:' . $e->getModel();
        }

        $data = Env::get('app.debug') ? $e->getTraceAsString() : '';
        return json(['code' => $code, 'msg' => $msg, 'data' => $data]);
    }

}
